package com.rjwh.dingdong.client.download;

import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.DownloadInfo;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.FileUtils;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.ZipUtils;

import java.io.File;
import java.util.List;

/**
 * 项目名称：whrjwh-parent
 * 类描述：
 * 创建人：wby
 * 创建时间：2016/9/23 10:54
 * 修改人：wby
 * 修改时间：2016/9/23 10:54
 * 修改备注：
 *
 * @version V1.0
 */
public class ZipUnTask implements Runnable {
    private DownloadInfo mDownloadInfo;

    public  ZipUnTask(DownloadInfo mDownloadInfo){
        this.mDownloadInfo = mDownloadInfo;
    }
    public void unzip(File srcFile, File descFile) {
        try {
            mDownloadInfo.setNetworkSpeed(0);
            mDownloadInfo.setDownloadState(DownloadManager.STATE_UNZIP);
            RxBus.get().post(LocalConstant.RX__POST_DOWNLOAD_STATE_UNZIP, mDownloadInfo);
            long start = System.currentTimeMillis();
            FileUtils.deleteFile(descFile);
            LogUtil.i("HTML", "解压文件地址:" + srcFile.getPath() + " 解压路径:" + descFile.getPath());
            ZipUtils.UnZipFolder(srcFile.getPath(), descFile.getPath());
            if (descFile.exists()) {
                LogUtil.i("HTML", mDownloadInfo.getGameId() + " 解压成功 耗费时间：" + (System.currentTimeMillis() - start));
                mDownloadInfo.setDownloadState(DownloadManager.STATE_UNZIP_SUCESS);
                inserlLocal(descFile);
                DownloadManager.getInstance().removeTask(mDownloadInfo.getGameId());
            } else {
                LogUtil.i("HTML", "解压目录为空" + mDownloadInfo.getGameId() + " 解压失败");
                mDownloadInfo.setDownloadState(DownloadManager.STATE_ERROR);
                mDownloadInfo.setProgress(0);
                MyApplication.db.update(mDownloadInfo, "gameId=" + mDownloadInfo.getGameId()+" and type="+mDownloadInfo.getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.i("HTML", mDownloadInfo.getGameId() + " 解压失败");
            FileUtils.deleteFile(descFile);
            mDownloadInfo.setDownloadState(DownloadManager.STATE_ERROR);
            mDownloadInfo.setProgress(0);
            mDownloadInfo.setNetworkSpeed(0);
            MyApplication.db.update(mDownloadInfo, "gameId=" + mDownloadInfo.getGameId()+" and type="+mDownloadInfo.getType());
        } finally {
            //无论解压成功还是失败都删除目标原文件
            FileUtils.deleteFile(srcFile);
            RxBus.get().post(LocalConstant.RX__POST_DOWNLOAD_STATE_UNZIP, mDownloadInfo);
        }
    }

    public void inserlLocal(File descFile) {
        List<LocalGmaeInfo> dbGamelist = MyApplication.db
                .findAllByWhere(
                        LocalGmaeInfo.class,
                        "pid="
                                + mDownloadInfo
                                .getGameId());
        if (dbGamelist.isEmpty()) {
            LocalGmaeInfo gminfo = new LocalGmaeInfo();
            gminfo.setType(mDownloadInfo.getType());
            gminfo.setPid(mDownloadInfo.getGameId());
            gminfo.setPname(mDownloadInfo
                    .getGameName());
            gminfo.setDownloadUrl(mDownloadInfo.getUrl());
            gminfo.setIsUpdate(0);
            gminfo.setPath(descFile.getPath());
            gminfo.setPic(mDownloadInfo.getGamePic());
            gminfo.setVersion(mDownloadInfo.getVersion());
            gminfo.setTotalLength(mDownloadInfo.getTotalLength());
            MyApplication.db.save(gminfo);
            LogUtil.i("HTML", "保存本地game");
        } else {
            LocalGmaeInfo gminfo = dbGamelist.get(0);
            gminfo.setIsUpdate(0);
            gminfo.setVersion(mDownloadInfo.getVersion());
            gminfo.setPname(mDownloadInfo.getGameName());
            gminfo.setPath(descFile.getPath());
            gminfo.setDownloadUrl(mDownloadInfo.getUrl());
            gminfo.setType(mDownloadInfo.getType());
            gminfo.setVersion(mDownloadInfo.getVersion());
            gminfo.setTotalLength(mDownloadInfo.getTotalLength());
            gminfo.setPic(mDownloadInfo.getGamePic());
            MyApplication.db.update(gminfo,"pid="+mDownloadInfo.getGameId()+" and type="+mDownloadInfo.getType());
            LogUtil.i("HTML", "更新本地game");
        }
    }
    @Override
    public void run() {
        File descFile = null;
        if (mDownloadInfo.getType() == 1) {
            descFile = new File(FileUtils.getDrwabookDirDirectory() + mDownloadInfo.getGameId() + File.separator);
        } else if (mDownloadInfo.getType() == 2) {
            descFile = new File(FileUtils.getGameDirectory() + mDownloadInfo.getGameId() + File.separator);
        } else {
            descFile = new File(FileUtils.getGameDirectory() + mDownloadInfo.getGameId() + File.separator);
        }
        unzip(new File(mDownloadInfo.getTargetPath()), descFile);
    }
}
